package com.example.satokendemospringboot;

import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.jwt.SaJwtTemplate;
import cn.dev33.satoken.jwt.SaJwtUtil;
import cn.dev33.satoken.jwt.StpLogicJwtForSimple;
import cn.dev33.satoken.stp.StpLogic;
import cn.hutool.jwt.JWT;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

@SpringBootApplication
public class SaTokenDemoSpringbootApplication {

    public static void main(String[] args) {
        System.out.println("启动成功，Sa-Token 配置如下：" + SaManager.getConfig());
        SpringApplication.run(SaTokenDemoSpringbootApplication.class, args);
    }


    @Configuration
    public class SaTokenConfigure {
        // Sa-Token 整合 jwt (Simple 简单模式)
        @Bean
        public StpLogic getStpLogicJwt() {
            return new StpLogicJwtForSimple();
        }
    }

    /**
     * 自定义 SaJwtUtil 生成 token 的算法
     */
    @PostConstruct
    public void setSaJwtTemplate() {
        SaJwtUtil.setSaJwtTemplate(new SaJwtTemplate() {
            @Override
            public String generateToken(JWT jwt, String keyt) {
                System.out.println("------ 自定义了 token 生成算法");
                Map<String,Object> param = new HashMap<>();
                param.put("userId","1001");
                param.put("userName","admin");
                param.put("tenantId","1000001");
                jwt.addPayloads(param);
                return super.generateToken(jwt, keyt);
            }
        });
    }

}
